$(function(){
    // 获取
    let objs={
        key:"",
        type:"",
        state:"",
        page:"1",
        perpage:"6",
    }
    function list(){
         $.ajax({
         type:'get', 
         url: 'admin/article/query',
         data:objs,
         success:function(res){
         console.log(res)
         const trrs=template(`trs`,res.data)
         $(`tbody`).html(trrs)
         let totalCount= res.data.totalCount
         layerpager(totalCount)
         }
         })

    }
    list()
    // 添加文章类别
function cc(){
    $.ajax({
        url:"admin/category/list",
        type:"get",
        success:function(res){
            let temp=template(`cate-lis`,res)
            $(`#selCategory`).html(temp)
        }
    })
}
cc()
    // 筛选文章类别
    $(`#btnSearch`).on(`click`,function(e){
        e.preventDefault()
        objs.type= $(`#selCategory`).val()
        objs.state= $(`#selStatus`).val()
        objs.page=1
        list()
    })
    // 分页器
function layerpager(res){
    layui.use('laypage',function(){
        let laypage=layui.laypage
        laypage.render({
            elem:'pager',
            limit:objs.perpage,
            count:res,
           curr:objs.page,
            jump: function(obj, first){
                console.log(obj)
                objs.page=obj.curr
                //obj包含了当前分页的所有参数，比如：
                console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                // console.log(obj.limit); //得到每页显示的条数
                
                //首次不执行
                if(!first){
                  //do something
                  list()
                }
              }
        })
    })
}

//删除
    $(`tbody`).on(`click`,`.delete`,function(){
       let id= $(this).attr(`data-id`)
      
       if(confirm(`确定要删除？`)){
        $.ajax({
            type:"post",
            url:"admin/article/delete",
            data:{id},
            success:function(back){
                layer.msg(back.msg)
                list()
            }
        })
      
      }
        
    })

})